﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[RequireComponent(typeof(BoatMovementController))]
[RequireComponent(typeof(BoatAnimationController))]
[RequireComponent(typeof(SwimmerController))]

public class BoatController : MonoBehaviour {

    public enum State
    {
        FISHING,
        BOATING,
        SWIMMING,
        DEAD
    };
    public State currentState = State.FISHING;

	public static bool isDead;
    SwimmerController swimmerController;
    BoatAnimationController animationController;
    BoatMovementController movimentController;
    public float vertical;
    public float horizontal;
    bool fishing = false;

	public Slider fishBar;
    public GameObject comebackMessage;
    //public GameObject isDeadMessage;
    public GameObject endgame;
    public GameObject score;
    public GameObject nameInput;
    public GameObject fishCounter;
    public GameObject fateMessage;
    bool isFishing = false;
    int fishes;
    //int maxFishes = 4;
    float fishingCatchTime;
    float thisFishingCatchTime;

    public void setState(State state)
    {
        //STOP OLD STATE
        switch (currentState)
        {
            default: break;
        }

        switch (state)
        {
            case State.SWIMMING: startSwimming(); break;
            case State.BOATING: startBoating(); break;
            case State.FISHING: startFishing(); break;
            case State.DEAD: setDead(); break;
        }
        currentState = state;
    }

	// Use this for initialization
	void Start () {
		isDead = false;
		fishes = 0;
		fishingCatchTime = Random.Range(6.0f,10.0f);
		fishBar.maxValue = fishingCatchTime;
		thisFishingCatchTime = fishingCatchTime;
		fishCounter.GetComponent<Text>().text = fishes.ToString();
        animationController = GetComponent<BoatAnimationController>();
        movimentController = GetComponent<BoatMovementController>();
        swimmerController = GetComponent<SwimmerController>();
        swimmerController.enabled = false;
        movimentController.enabled = false;
        setState(State.FISHING);
	}

    public void explode()
    {
        if(currentState == State.FISHING ||
            currentState == State.BOATING)
            setState(State.SWIMMING);

        else if (currentState == State.SWIMMING)
            setState(State.DEAD);


    }
	// Update is called once per frame
	void Update () {

        vertical = Input.GetAxis("Vertical");
        horizontal = Input.GetAxis("Horizontal");
        switch (currentState)
        {
            case State.FISHING: updateFishing(); break;
            case State.BOATING: updateBoating(); break;
            case State.SWIMMING: updateSwimming(); break;


        }
        
	}
    /*
    void OnTriggerEnter(Collider other)
    {
        Debug.Log(other.name);
        Debug.Break();
  
        if (other.tag == "Monster")
            explode();
    }*/
	void CountFish(){
		if (!isFishing) isFishing = !isFishing;
		fishingCatchTime -= Time.deltaTime * (Random.Range(0.0f,1.0f) < 0.3f? 3.0f: 0);
		fishBar.value = thisFishingCatchTime - fishingCatchTime;
		if (fishingCatchTime <= 0 && !comebackMessage.activeInHierarchy){
			fishes++;
			fishCounter.GetComponent<Text>().text = fishes.ToString();
			/*if (fishes == maxFishes && !comebackMessage.activeInHierarchy){
				comebackMessage.SetActive(true);
			}
			else{*/
				fishingCatchTime = Random.Range(6.0f,10.0f);
				thisFishingCatchTime = fishingCatchTime;
				fishBar.maxValue = thisFishingCatchTime;
			//}
		}
	}

    //----------------------------FISHING---------------------------------
    void startFishing()
    {
        animationController.setFishing(true);
        isFishing = true;
    }
    void updateFishing()
    {
        if (vertical != 0 || horizontal != 0)
        {
            setState(State.BOATING);
        }
        movimentController.vertical = 0;
        movimentController.horizontal = 0;
        CountFish();
    }

    //----------------------------BOATING---------------------------------
    void startBoating()
    {
        movimentController.enabled = true;
        swimmerController.enabled = false;

        animationController.setFishing(false);
        isFishing = false;
        fishingCatchTime = thisFishingCatchTime;
        fishBar.value = 0;
    }
    void updateBoating()
    {
        if (vertical == 0 && horizontal == 0)
        {
            setState(State.FISHING);
        }
        if (animationController.ready)
        {
            movimentController.vertical = vertical;
            movimentController.horizontal = horizontal;
        }
    }

    //----------------------------  SWIMMING---------------------------------
    void startSwimming()
    {
        movimentController.enabled = false;
        swimmerController.enabled = true;
        Debug.Log("StartSwimming");
    }
    void updateSwimming()
    {
        if (horizontal != 0 || vertical != 0)
        {
            swimmerController.direction.x = horizontal;
            swimmerController.direction.z = vertical;
        }

        swimmerController.direction.Normalize();
        Debug.Log("Swim");
    }

    //----------------------------  DEAD---------------------------------
    void setDead()
    {
    	isDead = true;
    	AudioController.audio_controller.GetSound("MonsterGrowlLong").Play ();
        Debug.Log("DEAD!");
        endgame.SetActive(true);
        fateMessage.GetComponent<Text>().text = "You got "+fishes+" fishes before meeting your fate.";
        //isDeadMessage.SetActive(true);
        
        //StartCoroutine("reset");
    }
    IEnumerator reset()
    {
        yield return new WaitForSeconds(9);
        Application.LoadLevel(Application.loadedLevel);
    }
    
    public void GoToMenu(){
    	Application.LoadLevel(0);
    }
    public void setScoreData(){
    	score.GetComponent<ScoreSender>().setData(fishes,nameInput.GetComponent<InputField>().text);
    }
}